#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"

u8 RM3xx_otptime = 0;

/*RM9300 Gamma寄存器位址*/
u8 RM300_Gamma_r[56]=
{
    0x00,0x01,  //0
    0x02,0x03,  //1
    0x04,0x05,  //2
    0x06,0x07,  //3
    0x08,0x09,  //7
    0x0A,0x0B,  //1
    0x0c,0x0D,  //15
    0x0e,0x0f,  //19
    0x10,0x11,  //23
    0x12,0x13,  //27
    0x14,0x15,  //31
    0x16,0x17,  //35
    0x18,0x19,  //39
    0x1a,0x1b,  //47
    0x1C,0x1D,  //55
    0x1e,0x1f,  //63
    0x20,0x21,  //79
    0x22,0x23,  //95
    0x24,0x25,  //111
    0x26,0x27,  //127
    0x28,0x29,  //143
    0x2a,0x2b,  //159
    0x30,0x31,  //175
    0x32,0x33,  //191
    0x34,0x35,  //207
    0x36,0x37,  //223
    0x38,0x39,  //239
    0x3A,0x3B   //255
};
u8 RM300_Gamma_g[56]=
{
    0x40,0x41,
    0x42,0x43,
    0x44,0x45,
    0x46,0x47,
    0x48,0x49,
    0x4A,0x4B,
    0x4c,0x4d,
    0x4E,0x4F,
    0x50,0x51,
    0x52,0x53,
    0x58,0x59,
    0x5a,0x5b,
    0x5c,0x5d,
    0x5e,0x5f,
    0x60,0x61,
    0x62,0x63,
    0x64,0x65,
    0x66,0x67,
    0x68,0x69,
    0x6A,0x6B,
    0x6C,0x6D,
    0x6E,0x6F,
    0x70,0x71,
    0x72,0x73,
    0x74,0x75,
    0x76,0x77,
    0x78,0x79,
    0x7A,0x7b
};
u8 RM300_Gamma_b[56]={
    0x7c,0x7D,
    0x7E,0x7f,
    0x80,0x81,
    0x82,0x83,
    0x84,0x85,
    0x86,0x87,
    0x88,0x89,
    0x8A,0x8b,
    0x8c,0x8d,
    0x8e,0x8f,
    0x90,0x91,
    0x92,0x93,
    0x94,0x95,
    0x96,0x97,
    0x98,0x99,
    0x9A,0x9b,
    0x9c,0x9d,
    0x9e,0x9f,
    0xa4,0xa5,
    0xa6,0xa7,
    0xac,0xad,
    0xae,0xaf,
    0xb0,0xb1,
    0xb2,0xb3,
    0xb4,0xb5,
    0xb6,0xb7,
    0xb8,0xb9,
    0xbA,0xbb    
};

/********************************************************************************************
********************************************************************************************
    RM69300  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM69300  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM69300  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM69300  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM69300  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
********************************************************************************************
********************************************************************************************/
void RM693xx_Write_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    SSD2828_W_Reg(SigMode,channel,0xB7,0x035b);
		//SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
    delay_ms(5); 
    SSD2828_W_Array(SigMode,channel,buffer,2);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    delay_ms(5);
    buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok
}

void RM693xx_Read_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[2];
    SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0xFFFD)|0x0080);  
    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);                                
    buffer1[0] = 0x01;
    buffer1[1] = buffer[3];
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
    delay_ms(20);
    buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    delay_ms(5);
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok  
}

void RM693xx_Write_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[5];
    //SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFfd);
		SSD2828_W_Reg(SigMode,channel,0xB7,0x035b);  //09->0b ,解决进高速闪屏
		delay_ms(5);

    if((buffer[3]&0x03) == 0x00)
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
				delay_ms(5);
        buffer1[0] = 0x02;
        buffer1[1] = 0x38;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
				delay_ms(5);
        buffer1[0] = 0x02;
        buffer1[1] = 0x53;
        buffer1[2] = 0x20;
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
				delay_ms(5);
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x05;              // GAMMA1  page1
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }
    else if((buffer[3]&0x03) == 0x01)
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
				delay_ms(5);
        buffer1[0] = 0x02;
        buffer1[1] = 0x53;
        buffer1[2] = 0x20;
        SSD2828_W_Array(SigMode,channel,buffer1,0);  
				delay_ms(5);
        buffer1[0] = 0x02;
        buffer1[1] = 0x39;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);   
				delay_ms(5);
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x02;              // GAMMA2  page7
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }   
    else if((buffer[3]&0x03) == 0x02)
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
				delay_ms(5);
        buffer1[0] = 0x02;
        buffer1[1] = 0x38;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
				delay_ms(5);
        buffer1[0] = 0x02;
        buffer1[1] = 0x53;
        buffer1[2] = 0x60;
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
				delay_ms(5);
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x03;              // GAMMA3  page8
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }
		else
		{
				buffer[4] = Uart_Error_Oth;
				STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok  				
				goto RM69300_w_end;
		}
    buffer1[0] = 0x02;
    buffer1[1] = 0xEF;
    buffer1[2] = 0x01;   
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(10);
    for( i = 0;i<buffer[2]-1;i++) 
    {	 
        if((buffer[3]&0xf0)==0x00)	
            SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM300_Gamma_r[i]);  
        else if((buffer[3]&0xf0)==0x40)
            SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM300_Gamma_g[i]); 
        else if((buffer[3]&0xf0)==0x80)
            SSD2828_W_Reg(SigMode,channel,0xBF,(u16)(buffer[i+4]<<8)|RM300_Gamma_b[i]); 																		

        delay_ms(3);
    }
    buffer1[0] = 0x02;
    buffer1[1] = 0xF0;
    buffer1[2] = 0x01;    
	  SSD2828_W_Array(SigMode,channel,buffer1,0);
    delay_ms(2);
		buffer1[0] = 0x02;
    buffer1[1] = 0xEF;
    buffer1[2] = 0x00;    
		SSD2828_W_Array(SigMode,channel,buffer1,0);    

    delay_ms(2);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok  
		RM69300_w_end: ;
}


void RM693xx_Read_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[3];
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15);
    if(buffer[4] == 0x00)
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        buffer1[0] = 0x02;
        buffer1[1] = 0x38;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0); 

        buffer1[0] = 0x02;
        buffer1[1] = 0x53;
        buffer1[2] = 0x20;
        SSD2828_W_Array(SigMode,channel,buffer1,0);          

        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x05;      // GAMMA1  page1
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
    }
    else if(buffer[4] == 0x01)
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        buffer1[0] = 0x02;
        buffer1[1] = 0x53;
        buffer1[2] = 0x20;
        SSD2828_W_Array(SigMode,channel,buffer1,0);  

        buffer1[0] = 0x02;
        buffer1[1] = 0x39;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);   

        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;       
        buffer1[2] = 0x02;      // GAMMA2  page7
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
    }
    else if(buffer[4] == 0x02)
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);	
        buffer1[0] = 0x02;
        buffer1[1] = 0x38;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);  
        buffer1[0] = 0x02;
        buffer1[1] = 0x53;
        buffer1[2] = 0x60;
        SSD2828_W_Array(SigMode,channel,buffer1,0);         
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;        
        buffer1[2] = 0x03;      // GAMMA3  page8
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
    }
		else
		{
				buffer[4] = Uart_Error_Oth;
				STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok  				
				goto RM69300_r_end;
		}		
		delay_ms(15);	
    buffer1[0] = 0x02;
    buffer1[1] = 0xEF;
    buffer1[2] = 0x01;   
		SSD2828_W_Array(SigMode,channel,buffer1,0);		
    delay_ms(5);		
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
    SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);	
    for( i = 0;i<buffer[2]-1;i++)			 	
    {  	
        
        SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);                                
        buffer1[0] = 0x01;
        buffer1[1] = (buffer[3]==0x00) ? RM300_Gamma_r[i] : ((buffer[3]==0x42) ? RM300_Gamma_g[i] : RM300_Gamma_b[i]);
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
				delay_ms(6);
        buffer[4+i]=SSD2828_R_Reg(SigMode,channel,0xFF);        
    }
    buffer1[0] = 0x02;
    buffer1[1] = 0xEF;
    buffer1[2] = 0x00;   
		SSD2828_W_Array(SigMode,channel,buffer1,0);		

    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 Gamma数据  ：ok  
		RM69300_r_end: ;
}

void RM693xx_Gamma_OTP_Start(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data,u8 RM3xx_otptime)
{
    u8 buffer1[3];  
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);   
    delay_ms(5);
    buffer1[0] = 0x02;
    buffer1[1] = 0xFE;
    buffer1[2] = 0x00;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     // UCS
    delay_ms(5);
    buffer1[0] = 0x01;
    buffer1[1] = 0x28;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     // 关显示
    delay_ms(100);
    buffer1[0] = 0x01;
    buffer1[1] = 0x10;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     // 关显示     
    delay_ms(100);  //otp
    buffer1[0] = 0x02;
    buffer1[1] = 0xFE;
    buffer1[2] = 0x04;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     //Page0
    
    if(buffer[1]==0x0A)  //otp GammaSet1
    {
        buffer1[0] = 0x02;
        if(RM3xx_otptime == 1)      // 1st
        {
            buffer1[1] = 0xF7;
            buffer1[2] = 0x02;
        }
        else if(RM3xx_otptime == 2) // 2nd
        {
            buffer1[1] = 0xF9;
            buffer1[2] = 0x02;        
        }
        SSD2828_W_Array(SigMode,channel,buffer1,0);     //OTP_CMD2_P1_PGM_EN          
    }
		if(buffer[1]==0x07)
		{
				if(buffer[0]==RM69300)
				{
						SSD2828_W_Reg(SigMode,channel,0xBF,(RM3xx_otptime==0x01) ? 0x20F6 : 0x20F7);//spr
				}	
		}
    if(buffer[1]==0x0D)  //otp GammaSet2
    {
        buffer1[0] = 0x02;
        if(RM3xx_otptime == 1)      // 1st
        {
            buffer1[1] = 0xF7;
            buffer1[2] = 0x80;
        }
        else if(RM3xx_otptime == 2) // 2nd
        {
            buffer1[1] = 0xF9;
            buffer1[2] = 0x08;        
        }
        SSD2828_W_Array(SigMode,channel,buffer1,0);     //OTP_CMD2_P7_PGM_EN  
    }
    if(buffer[1]==0x0E) //otp GammaSet3
    {		
        buffer1[0] = 0x02;
        if(RM3xx_otptime == 1)      // 1st
        {
            buffer1[1] = 0xF8;
            buffer1[2] = 0x01;
        }
        else if(RM3xx_otptime == 2) // 2nd
        {
            buffer1[1] = 0xF9;
            buffer1[2] = 0x10;        
        }        
        SSD2828_W_Array(SigMode,channel,buffer1,0);     //OTP_CMD2_P8_PGM_EN  
    }
    if(buffer[1]==0x0F) // otp goa
    {
        buffer1[0] = 0x02;
        if(RM3xx_otptime == 1)      // 1st
        {
            buffer1[1] = 0xF7;
            buffer1[2] = 0x04;;
        }
        else if(RM3xx_otptime == 2) // 2nd
        {
            buffer1[1] = 0xF9;
            buffer1[2] = 0x04;        
        }
        else if(RM3xx_otptime == 3) // 3rd
        {
            buffer1[1] = 0xF9;
            buffer1[2] = 0x40;        
        }
        SSD2828_W_Array(SigMode,channel,buffer1,0);     //OTP_CMD2_P2_PGM_EN  
    }
    if(buffer[1]==0x10) // otp power
    {
        buffer1[0] = 0x02;        
        if(RM3xx_otptime == 1)      // 1st
        {
            buffer1[1] = 0xF7;
            buffer1[2] = 0x01;
        }
        else if(RM3xx_otptime == 2) // 2nd
        {
            buffer1[1] = 0xF9;
            buffer1[2] = 0x01;        
        }
        else if(RM3xx_otptime == 3) // 3rd
        {
            buffer1[1] = 0xF9;
            buffer1[2] = 0x80;        
        }        
        SSD2828_W_Array(SigMode,channel,buffer1,0);     //OTP_CMD2_P0_PGM_EN  
    }    

    buffer1[0] = 0x02;
    buffer1[1] = 0xF2;
    buffer1[2] = 0x03;
    SSD2828_W_Array(SigMode,channel,buffer1,0);    
	
    buffer1[0] = 0x02;
    buffer1[1] = 0xF3;
    buffer1[2] = 0xA5;
    SSD2828_W_Array(SigMode,channel,buffer1,0);  

    buffer1[0] = 0x02;
    buffer1[1] = 0xF4;
    buffer1[2] = 0x5A;
    SSD2828_W_Array(SigMode,channel,buffer1,0);  

    buffer1[0] = 0x02;
    buffer1[1] = 0xF5;
    buffer1[2] = 0x3C;
    SSD2828_W_Array(SigMode,channel,buffer1,0);  		
                      
    delay_ms(1500);         // wait for >400 ms   

    buffer[4] = Uart_Error_Oth;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写OTP状态  ：ok
}

void RM693xx_Read_OTP_Time(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[3];
    u16 ED_D,EE_D,EF_D;
    u8 RM300_otptime = 0;
    if(SigMode != CMD_Mode)
        SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    delay_ms(5);
    buffer1[0] = 0x02;
    buffer1[1] = 0xFE;
    buffer1[2] = 0x04;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     // Page 0
		
    buffer1[0] = 0x02;
    buffer1[1] = 0xF2;
    buffer1[2] = 0x80;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     // otp_load_en 
		
    buffer1[0] = 0x02;
    buffer1[1] = 0xF6;
    buffer1[2] = 0x03;
    SSD2828_W_Array(SigMode,channel,buffer1,0);     // Margin Read Check      
    SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);      

    delay_ms(5);
    buffer1[0] = 0x01;
    buffer1[1] = 0xED;
    SSD2828_W_Array(SigMode,channel,buffer1,0);
    delay_ms(20);
    if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI  
        SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);							
    delay_ms(5);                                  
    ED_D=SSD2828_R_Reg(SigMode,channel,0xFF); 
    
    
    buffer1[0] = 0x01;
    buffer1[1] = 0xEE;
    SSD2828_W_Array(SigMode,channel,buffer1,0);
    delay_ms(20);
    if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI  
        SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);							
    delay_ms(5);                             
    EE_D=SSD2828_R_Reg(SigMode,channel,0xFF); 
    
    buffer1[0] = 0x01;
    buffer1[1] = 0xEF;
    SSD2828_W_Array(SigMode,channel,buffer1,0);
    delay_ms(20);
    if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI  
        SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);							
    delay_ms(5);                               
    EF_D=SSD2828_R_Reg(SigMode,channel,0xFF);     
    if(buffer[3] == 0x00)       //读取gamma1次数
    {
        if(ED_D&0X02)
            RM300_otptime = 1;
        if(EE_D&0X10)
            RM300_otptime = 2;
    }
    else if(buffer[3] == 0x01)  //读取gamma2次数
    {
        if(ED_D&0X02)
            RM300_otptime = 1; 
        if(EE_D&0X40)
            RM300_otptime = 2;        
    }
    else if(buffer[3] == 0x02)  //读取gamma3次数
    {
        if(EE_D&0X01)
            RM300_otptime = 1;
        if(EE_D&0X80)
            RM300_otptime = 2;        
    }
    else if(buffer[3] == 0x03)  //读取GOA次数
    {
        if(ED_D&0X04)
            RM300_otptime = 1;
        if(EE_D&0X20)
            RM300_otptime = 2;  
        if(EF_D&0X80)
            RM300_otptime = 3;
        
    }
    else if(buffer[3] == 0x04)  //读取POWER次数
    {
        if(ED_D&0X01)
            RM300_otptime = 1;
        if(EE_D&0X08)
            RM300_otptime = 2;  
        if(EF_D&0X40)
            RM300_otptime = 3;   
    }

                                    
    buffer[4]=RM300_otptime;
    RM300_otptime=0x00;
    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[3] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的OTP数据  ：ok  
}

void RM693xx_Gamma_switch(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[5];
    static u8 RM69300_state = 0;    // 0=normal   1=idle  2=HBM
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    delay_ms(10);
    if(buffer[3] == 0x01)
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        if(RM69300_state == 2)
        {
            buffer1[0] = 0x02;
            buffer1[1] = 0x53;
            buffer1[2] = 0x20;         //Brightness control is on
            SSD2828_W_Array(SigMode,channel,buffer1,0);
        }
        else if(RM69300_state == 1)
        {
            buffer1[0] = 0x02;
            buffer1[1] = 0x38;
            buffer1[2] = 0x00;         //Exit idle 
            SSD2828_W_Array(SigMode,channel,buffer1,0);  
        }  
        
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x05;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        RM69300_state = 0;
    }
    else if(buffer[3] == 0x02)
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        if(RM69300_state == 2)
        {
            buffer1[0] = 0x02;
            buffer1[1] = 0x53;
            buffer1[2] = 0x20;
            SSD2828_W_Array(SigMode,channel,buffer1,0);
        }
        buffer1[0] = 0x02;
        buffer1[1] = 0x39;
        buffer1[2] = 0x00;            //Enter idle
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x02;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        RM69300_state = 1;
       
    }   
    else if(buffer[3] == 0x03)
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        if(RM69300_state == 1)
        {
            buffer1[0] = 0x02;
            buffer1[1] = 0x38;
            buffer1[2] = 0x00;
            SSD2828_W_Array(SigMode,channel,buffer1,0);
        }
        buffer1[0] = 0x02;
        buffer1[1] = 0x53;
        buffer1[2] = 0x60;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x03;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        RM69300_state = 2;
    }
    
    delay_ms(50);
    buffer1[0] = 0x02;
    buffer1[1] = 0xEF;
    buffer1[2] = 0x01;
    SSD2828_W_Array(SigMode,channel,buffer1,0);
    delay_ms(50);    
    if(buffer[4] == 0x01) 
    {
        buffer1[0] = 0x02;
        buffer1[1] = 0xBC;
        buffer1[2] = 0x00;                  // 28绑点
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }
    else if(buffer[4] == 0x02)
    {
    
        buffer1[0] = 0x02;
        buffer1[1] = 0xBC;
        buffer1[2] = 0x01;                  // 15绑点
        SSD2828_W_Array(SigMode,channel,buffer1,0);
    }
		delay_ms(50);
		buffer1[0] = 0x02;
		buffer1[1] = 0xF0;
		buffer1[2] = 0x01;    
		SSD2828_W_Array(SigMode,channel,buffer1,0);
		buffer1[0] = 0x02;
		buffer1[1] = 0xEF;
		buffer1[2] = 0x00;    
		SSD2828_W_Array(SigMode,channel,buffer1,0);     
		delay_ms(5);
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		buffer[4] = Uart_Error_None;
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok  
}

void RM693xx_Write_51Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
    SSD2828_W_Array(SigMode,channel,buffer,2);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}



void ProcessForIc2C( USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//void *pdev;
	switch(buffer[1])
	{
			case 0x01:                                      //写寄存器
				RM693xx_Write_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
				break;
			case 0x02:                                      //读寄存器
				RM693xx_Read_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
				break;
			case 0x08:                                      //写Gamma
				RM693xx_Write_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
				break;
			case 0x09:                                      //读Gamma
				RM693xx_Read_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data); 
				break;
			case 0x0A:			//otp GammaSet1					//OTP Start
			case 0x0D:          //otp GammaSet2    
			case 0x0E:          //otp GammaSet3
			case 0x0F:          //otp goa
			case 0x10:          //otp power
			case 0x07:
				RM693xx_Gamma_OTP_Start(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data,RM3xx_otptime); 
				break;
			case 0x0B:                                      //Read OTP Time
				RM693xx_Read_OTP_Time(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data); 
				break;
			case 0x0C://otp time

				RM3xx_otptime=USB_Rx_Buffer[4];
				USB_Rx_Buffer[4] = Uart_Error_None;
				STM2PC_RM671xx(pdev,CDC_IN_EP,USB_Rx_Buffer,USB_Rx_Buffer[2]+3);           //返回 写otp time
				break;
			case 0xDD:                                      // auto Gamma
			//    RM693xx_auto_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,CS_Master,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
				break;
			case 0x11:                                      // auto Gamma
				RM693xx_Gamma_switch(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
				break;
			case 0x51:                                      //写寄存器
				RM693xx_Write_51Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
				break;
				default:break;
	}			
}


